import { request, encapsulation } from './request'

/*
 *@description: 获取小说分类
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:32:18
 */
export function getBookClassification() {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query:
        'db.collection("boxBookClassification").where({classificationStatus :1}).limit(1000).get()'
    }
  })
}
/*
 *@description: 单独获取token方法
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:32:45
 */
export function getToken() {
  // 先获取token
  return request({
    url: `/api/cgi-bin/token?grant_type=client_credential&appid=wx38373392939e805c&secret=c5b8c5d42ea638a695f51f3dabb3813a`,
    method: 'get'
  })
}
/*
 *@description:  上传文件
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:33:02
 */
export function uploadImg(option) {
  return encapsulation({
    url: `/api/tcb/uploadfile`,
    method: 'post',
    data: {
      path: option
    }
  })
}
/*
 *@description:上传真实文件
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:33:11
 */
export function uploadImgTrue(url, option) {
  return request({
    url,
    headers: {
      'Content-Type': 'multipart/form-data; boundary=KuTh71Gh'
    },
    method: 'post',
    data: option
  })
}
/*
 *@description: 书籍添加
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:33:21
 */
export function bookAdd(option) {
  return encapsulation({
    url: `/api/tcb/databaseadd`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .add({
        data: {
          bookName: '${option.bookName}',
          bookLaunchTime: new Date('${option.bookLaunchTime}'),
          bookIntroduction: '${option.bookIntroduction}',
          bookClassification: '${option.bookClassification}',
          bookAuthor: '${option.bookAuthor}',
          bookCover: '${option.bookCover}',
          authorId: '${option.authorId}',
          bookCollection: 0,
          bookEndOrSerial: 1,
          bookGeneralSection: 0,
          bookNumberOfHits: 0,
          bookNumberOfWords: 0,
          bookStatus: 1,
          bookToUpdate: new Date('${option.bookToUpdate}')
        }
      })`
    }
  })
}
/*
 *@description: 查询书籍方法
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-18 09:51:15
 */
export function bookQueryPage(option) {
  return encapsulation({
    url: `/api/tcb/databasequery`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .where({
      ${
        option.search
          ? `
        "$or": [
          {
            bookAuthor: db.RegExp({
              regexp: '${option.search}',
              options: 'i',
            })
          },
          {
            bookName: db.RegExp({
              regexp: '${option.search}',
              options: 'i',
            })
          }
        ],
      `
          : ''
      }
      ${option.authorId?`authorId:'${option.authorId}',`:``}
      bookStatus: _.neq(${option.bookStatus||3}),
    }).skip(${option.size * (option.page - 1)})
      .limit(${option.size})
      .get()`
    }
  })
}
/*
 *@description: 查询总数
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-18 22:59:17
 */
export function bookQueryTotal(option) {
  return encapsulation({
    url: `/api/tcb/databasecount`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .where({
      ${
        option.search
          ? `
        "$or": [
          {
            bookAuthor: db.RegExp({
              regexp: '${option.search}',
              options: 'i',
            })
          },
          {
            bookName: db.RegExp({
              regexp: '${option.search}',
              options: 'i',
            })
          }
        ],
      `
          : ''
      }
      ${option.authorId?`authorId:'${option.authorId}',`:``}
      bookStatus: _.neq(${option.bookStatus||3}),
    }).count()`
    }
  })
}
/*
 *@description: 修改图书信息
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-18 21:24:00
 */
export function bookModify(option) {
  return encapsulation({
    url: `/api/tcb/databaseupdate`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .where({_id:'${option._id}'}).update({
        data: {
          ${option.bookName ? `bookName:'${option.bookName}',` : ``}
          ${
            option.bookLaunchTime!=undefined && option.bookLaunchTime.date
              ? `bookLaunchTime:new Date('${option.bookLaunchTime.date}'),`
              : ``
          }
          ${
            option.bookIntroduction
              ? `bookIntroduction:'${option.bookIntroduction}',`
              : ``
          }
          ${
            option.bookClassification
              ? `bookClassification:'${option.bookClassification}',`
              : ``
          }
          ${option.bookAuthor ? `bookAuthor:'${option.bookAuthor}',` : ``}
          ${option.bookCover ? `bookCover:'${option.bookCover}',` : ``}
          ${option.authorId ? `authorId:'${option.authorId}',` : ``}
          ${
            option.bookCollection != undefined
              ? `bookCollection:${option.bookCollection},`
              : ``
          }
          ${
            option.bookEndOrSerial != undefined
              ? `bookEndOrSerial:${option.bookEndOrSerial},`
              : ``
          }
          ${
            option.bookGeneralSection != undefined
              ? `bookGeneralSection:${option.bookGeneralSection},`
              : ``
          }
          ${
            option.bookNumberOfHits != undefined
              ? `bookNumberOfHits:${option.bookNumberOfHits},`
              : ``
          }
          ${
            option.bookNumberOfWords != undefined
              ? `bookNumberOfWords:${option.bookNumberOfWords},`
              : ``
          }
          ${
            option.bookStatus != undefined
              ? `bookStatus:${option.bookStatus},`
              : ``
          }
          ${
            option.bookToUpdate!=undefined && option.bookToUpdate.date
              ? `bookToUpdate:new Date('${option.bookToUpdate.date}'),`
              : ``
          }
        }
      })`
    }
  })
}
/*
 *@functionName: queryBookById
 *@params1: 书籍id
 *@description:
 *@author: chendi
 *@date: 2020-08-23 10:05:16
 *@version: V1.0.5
 */
export function queryBookById(option) {
  return encapsulation({
    url: `/api/tcb/databasequery`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .where({
        bookStatus:1,
        _id:'${option}'
      })
      .get()
    `
    }
  })
}
/*
 *@functionName: 根据id删除图书
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-08-31 09:32:21
 *@version: V1.0.5
*/
export function removeBookById(option) {
  return encapsulation({
    url: `/api/tcb/databasedelete`,
    method: 'post',
    data: {
      query: `db.collection('boxBookDetails')
      .where({
        _id:'${option}'
      })
      .remove()
    `
    }
  })
}